package com.solid.rabbitmq.producer;

import com.solid.rabbitmq.bean.VipsDelayBean;
import com.solid.rabbitmq.config.DelayRabbitConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Package: com.kids.rabbitmq.producer
 * @Description:
 * @Author: Mr.z
 * @CreatDate: 2020/06/03
 */

@Component
public class DelayProducer {
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * VIP过期消息通知
     *
     * @param id
     * @param timestamp
     */
    public void sendVipDelay(Long id, Long timestamp) {
        VipsDelayBean vipsDelayBean = new VipsDelayBean();
        vipsDelayBean.setId(id);
        vipsDelayBean.setTimestamp(timestamp);
        this.amqpTemplate.convertAndSend(DelayRabbitConfig.ORDER_DELAY_EXCHANGE, DelayRabbitConfig.ORDER_DELAY_ROUTING_KEY, vipsDelayBean, message -> {
            // 如果配置了 params.put("x-message-ttl", 5 * 1000); 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
            message.getMessageProperties().setExpiration(timestamp + "");
            return message;
        });
    }
}
